<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
    <title>Penlight Documentation</title>
    <link rel="stylesheet" href="../ldoc_fixed.css" type="text/css" />
</head>
<body>

<div id="container">

<div id="product">
	<div id="product_logo"></div>
	<div id="product_name"><big><b></b></big></div>
	<div id="product_description"></div>
</div> <!-- id="product" -->


<div id="main">


<!-- Menu -->

<div id="navigation">
<br/>
<h1>Penlight</h1>

<ul>
  <li><a href="https://github.com/lunarmodules/Penlight">GitHub Project</a></li>
  <li><a href="../index.html">Documentation</a></li>
</ul>



<h2>Examples</h2>
<ul class="nowrap">
  <li><a href="../examples/seesubst.lua.html">seesubst.lua</a></li>
  <li><a href="../examples/sipscan.lua.html">sipscan.lua</a></li>
  <li><strong>symbols.lua</strong></li>
  <li><a href="../examples/test-cmp.lua.html">test-cmp.lua</a></li>
  <li><a href="../examples/test-data.lua.html">test-data.lua</a></li>
  <li><a href="../examples/test-listcallbacks.lua.html">test-listcallbacks.lua</a></li>
  <li><a href="../examples/test-pretty.lua.html">test-pretty.lua</a></li>
  <li><a href="../examples/test-symbols.lua.html">test-symbols.lua</a></li>
  <li><a href="../examples/testclone.lua.html">testclone.lua</a></li>
  <li><a href="../examples/testconfig.lua.html">testconfig.lua</a></li>
  <li><a href="../examples/testglobal.lua.html">testglobal.lua</a></li>
  <li><a href="../examples/testinputfields.lua.html">testinputfields.lua</a></li>
  <li><a href="../examples/testinputfields2.lua.html">testinputfields2.lua</a></li>
  <li><a href="../examples/testxml.lua.html">testxml.lua</a></li>
  <li><a href="../examples/which.lua.html">which.lua</a></li>
</ul>
<h2>Libraries</h2>
<ul class="nowrap">
  <li><a href="../libraries/pl.html">pl</a></li>
  <li><a href="../libraries/pl.app.html">pl.app</a></li>
  <li><a href="../libraries/pl.array2d.html">pl.array2d</a></li>
  <li><a href="../libraries/pl.class.html">pl.class</a></li>
  <li><a href="../libraries/pl.compat.html">pl.compat</a></li>
  <li><a href="../libraries/pl.comprehension.html">pl.comprehension</a></li>
  <li><a href="../libraries/pl.config.html">pl.config</a></li>
  <li><a href="../libraries/pl.data.html">pl.data</a></li>
  <li><a href="../libraries/pl.dir.html">pl.dir</a></li>
  <li><a href="../libraries/pl.file.html">pl.file</a></li>
  <li><a href="../libraries/pl.func.html">pl.func</a></li>
  <li><a href="../libraries/pl.import_into.html">pl.import_into</a></li>
  <li><a href="../libraries/pl.input.html">pl.input</a></li>
  <li><a href="../libraries/pl.lapp.html">pl.lapp</a></li>
  <li><a href="../libraries/pl.lexer.html">pl.lexer</a></li>
  <li><a href="../libraries/pl.luabalanced.html">pl.luabalanced</a></li>
  <li><a href="../libraries/pl.operator.html">pl.operator</a></li>
  <li><a href="../libraries/pl.path.html">pl.path</a></li>
  <li><a href="../libraries/pl.permute.html">pl.permute</a></li>
  <li><a href="../libraries/pl.pretty.html">pl.pretty</a></li>
  <li><a href="../libraries/pl.seq.html">pl.seq</a></li>
  <li><a href="../libraries/pl.sip.html">pl.sip</a></li>
  <li><a href="../libraries/pl.strict.html">pl.strict</a></li>
  <li><a href="../libraries/pl.stringio.html">pl.stringio</a></li>
  <li><a href="../libraries/pl.stringx.html">pl.stringx</a></li>
  <li><a href="../libraries/pl.tablex.html">pl.tablex</a></li>
  <li><a href="../libraries/pl.template.html">pl.template</a></li>
  <li><a href="../libraries/pl.test.html">pl.test</a></li>
  <li><a href="../libraries/pl.text.html">pl.text</a></li>
  <li><a href="../libraries/pl.types.html">pl.types</a></li>
  <li><a href="../libraries/pl.url.html">pl.url</a></li>
  <li><a href="../libraries/pl.utils.html">pl.utils</a></li>
  <li><a href="../libraries/pl.xml.html">pl.xml</a></li>
</ul>
<h2>Classes</h2>
<ul class="nowrap">
  <li><a href="../classes/pl.Date.html">pl.Date</a></li>
  <li><a href="../classes/pl.List.html">pl.List</a></li>
  <li><a href="../classes/pl.Map.html">pl.Map</a></li>
  <li><a href="../classes/pl.MultiMap.html">pl.MultiMap</a></li>
  <li><a href="../classes/pl.OrderedMap.html">pl.OrderedMap</a></li>
  <li><a href="../classes/pl.Set.html">pl.Set</a></li>
</ul>
<h2>Manual</h2>
<ul class="nowrap">
  <li><a href="../manual/01-introduction.md.html">Introduction</a></li>
  <li><a href="../manual/02-arrays.md.html">Tables and Arrays</a></li>
  <li><a href="../manual/03-strings.md.html">Strings. Higher-level operations on strings.</a></li>
  <li><a href="../manual/04-paths.md.html">Paths and Directories</a></li>
  <li><a href="../manual/05-dates.md.html">Date and Time</a></li>
  <li><a href="../manual/06-data.md.html">Data</a></li>
  <li><a href="../manual/07-functional.md.html">Functional Programming</a></li>
  <li><a href="../manual/08-additional.md.html">Additional Libraries</a></li>
  <li><a href="../manual/09-discussion.md.html">Technical Choices</a></li>
</ul>

</div>

<div id="content">

    <h2>symbols.lua</h2>
<pre>
<span class="global">require</span> <span class="string">'pl'</span>
utils.<span class="function-name">import</span> <span class="string">'pl.func'</span>
<span class="keyword">local</span> ops = <span class="global">require</span> <span class="string">'pl.operator'</span>
<span class="keyword">local</span> List = <span class="global">require</span> <span class="string">'pl.List'</span>
<span class="keyword">local</span> append,concat = <span class="global">table</span>.insert,<span class="global">table</span>.concat
<span class="keyword">local</span> compare,find_if,compare_no_order,imap,reduce,count_map = tablex.compare,tablex.find_if,tablex.compare_no_order,tablex.imap,tablex.reduce,tablex.count_map
<span class="keyword">local</span> unpack = <span class="global">table</span>.unpack

<span class="keyword">function</span> <span class="function-name">bindval</span> (self,val)
    <span class="global">rawset</span>(self,<span class="string">'value'</span>,val)
<span class="keyword">end</span>

<span class="keyword">local</span> optable = ops.optable

<span class="keyword">function</span> <span class="function-name">sexpr</span> (e)
	<span class="keyword">if</span> <span class="function-name">isPE</span>(e) <span class="keyword">then</span>
		<span class="keyword">if</span> e.op ~= <span class="string">'X'</span> <span class="keyword">then</span>
			<span class="keyword">local</span> args = tablex.<span class="function-name">imap</span>(sexpr,e)
			<span class="keyword">return</span> <span class="string">'('</span>..e.op..<span class="string">' '</span>..<span class="global">table</span>.<span class="function-name">concat</span>(args,<span class="string">' '</span>)..<span class="string">')'</span>
		<span class="keyword">else</span>
			<span class="keyword">return</span> e.repr
		<span class="keyword">end</span>
	<span class="keyword">else</span>
		<span class="keyword">return</span> <span class="global">tostring</span>(e)
	<span class="keyword">end</span>
<span class="keyword">end</span>


psexpr = <span class="function-name">compose</span>(<span class="global">print</span>,sexpr)



<span class="keyword">function</span> <span class="function-name">equals</span> (e1,e2)
    <span class="keyword">local</span> p1,p2 = <span class="function-name">isPE</span>(e1),<span class="function-name">isPE</span>(e2)
    <span class="keyword">if</span> p1 ~= p2 <span class="keyword">then</span> <span class="keyword">return</span> <span class="keyword">false</span> <span class="keyword">end</span>  <span class="comment">-- different kinds of animals!
</span>    <span class="keyword">if</span> p1 <span class="keyword">and</span> p2 <span class="keyword">then</span> <span class="comment">-- both PEs
</span>        <span class="comment">-- operators must be the same
</span>        <span class="keyword">if</span> e1.op ~= e2.op <span class="keyword">then</span> <span class="keyword">return</span> <span class="keyword">false</span> <span class="keyword">end</span>
        <span class="comment">-- PHs are equal if their representations are equal
</span>        <span class="keyword">if</span> e1.op == <span class="string">'X'</span> <span class="keyword">then</span> <span class="keyword">return</span> e1.repr == e2.repr
        <span class="comment">-- commutative operators
</span>        <span class="keyword">elseif</span> e1.op == <span class="string">'+'</span> <span class="keyword">or</span> e1.op == <span class="string">'*'</span> <span class="keyword">then</span>
            <span class="keyword">return</span> <span class="function-name">compare_no_order</span>(e1,e2,equals)
        <span class="keyword">else</span>
            <span class="comment">-- arguments must be the same
</span>            <span class="keyword">return</span> <span class="function-name">compare</span>(e1,e2,equals)
        <span class="keyword">end</span>
    <span class="keyword">else</span> <span class="comment">-- fall back on simple equality for non PEs
</span>        <span class="keyword">return</span> e1 == e2
    <span class="keyword">end</span>
<span class="keyword">end</span>

<span class="comment">-- run down an unbalanced operator chain (like a+b+c) and return the arguments {a,b,c}
</span><span class="keyword">function</span> <span class="function-name">tcollect</span> (op,e,ls)
    <span class="keyword">if</span> <span class="function-name">isPE</span>(e) <span class="keyword">and</span> e.op == op <span class="keyword">then</span>
        <span class="keyword">for</span> i = <span class="number">1</span>,#e <span class="keyword">do</span>
            <span class="function-name">tcollect</span>(op,e[i],ls)
        <span class="keyword">end</span>
    <span class="keyword">else</span>
        ls:<span class="function-name">append</span>(e)
        <span class="keyword">return</span>
    <span class="keyword">end</span>
<span class="keyword">end</span>

<span class="keyword">function</span> <span class="function-name">rcollect</span> (e)
    <span class="keyword">local</span> res = <span class="function-name">List</span>()
    <span class="function-name">tcollect</span>(e.op,e,res)
    <span class="keyword">return</span> res
<span class="keyword">end</span>


<span class="comment">-- balance ensures that +/* chains are collected together, operates in-place.
</span><span class="comment">-- thus (+(+ a b) c) or (+ a (+ b c)) becomes (+ a b c), order immaterial
</span><span class="keyword">function</span> <span class="function-name">balance</span> (e)
    <span class="keyword">if</span> <span class="function-name">isPE</span>(e) <span class="keyword">and</span> e.op ~= <span class="string">'X'</span> <span class="keyword">then</span>
        <span class="keyword">local</span> op,args = e.op
        <span class="keyword">if</span> op == <span class="string">'+'</span> <span class="keyword">or</span> op == <span class="string">'*'</span> <span class="keyword">then</span>
            args = <span class="function-name">rcollect</span>(e)
        <span class="keyword">else</span>
            args = <span class="function-name">imap</span>(balance,e)
        <span class="keyword">end</span>
        <span class="keyword">for</span> i = <span class="number">1</span>,#args <span class="keyword">do</span>
            e[i] = args[i]
        <span class="keyword">end</span>
    <span class="keyword">end</span>
    <span class="keyword">return</span> e
<span class="keyword">end</span>

<span class="comment">-- fold constants in an expression
</span><span class="keyword">function</span> <span class="function-name">fold</span> (e)
    <span class="keyword">if</span> <span class="function-name">isPE</span>(e) <span class="keyword">then</span>
        <span class="keyword">if</span> e.op == <span class="string">'X'</span> <span class="keyword">then</span>
            <span class="comment">-- there could be _bound values_!
</span>            <span class="keyword">local</span> val = <span class="global">rawget</span>(e,<span class="string">'value'</span>)
            <span class="keyword">return</span> val <span class="keyword">and</span> val <span class="keyword">or</span> e
        <span class="keyword">else</span>
            <span class="keyword">local</span> op = e.op
            <span class="keyword">local</span> addmul = op == <span class="string">'*'</span> <span class="keyword">or</span> op == <span class="string">'+'</span>
            <span class="comment">-- first fold all arguments
</span>            <span class="keyword">local</span> args = <span class="function-name">imap</span>(fold,e)
            <span class="keyword">if</span> <span class="keyword">not</span> addmul <span class="keyword">and</span> <span class="keyword">not</span> <span class="function-name">find_if</span>(args,isPE) <span class="keyword">then</span>
                <span class="comment">-- no placeholders in these args, we can fold the expression.
</span>                <span class="keyword">local</span> opfn = optable[op]
                <span class="keyword">if</span> opfn <span class="keyword">then</span>
                    <span class="keyword">return</span> <span class="function-name">opfn</span>(<span class="function-name">unpack</span>(args))
                <span class="keyword">else</span>
                    <span class="keyword">return</span> <span class="string">'?'</span>
                <span class="keyword">end</span>
            <span class="keyword">elseif</span> addmul <span class="keyword">then</span>
                <span class="comment">-- enforce a few rules for + and *
</span>                <span class="comment">-- split the args into two classes, PE args and non-PE args.
</span>                <span class="keyword">local</span> classes = List.<span class="function-name">partition</span>(args,isPE)
                <span class="keyword">local</span> pe,npe = classes[<span class="keyword">true</span>],classes[<span class="keyword">false</span>]
                <span class="keyword">if</span> npe <span class="keyword">then</span> <span class="comment">-- there's at least one non PE argument
</span>                    <span class="comment">-- so fold them
</span>                    <span class="keyword">if</span> #npe == <span class="number">1</span> <span class="keyword">then</span> npe = npe[<span class="number">1</span>]
                    <span class="keyword">else</span> npe = npe:<span class="function-name">reduce</span>(optable[op])
                    <span class="keyword">end</span>
                    <span class="comment">-- if the result is a constant, return it
</span>                    <span class="keyword">if</span> <span class="keyword">not</span> pe <span class="keyword">then</span> <span class="keyword">return</span> npe <span class="keyword">end</span>

                    <span class="comment">-- either (* 1 x) =&gt; x or (* 1 x y ...) =&gt; (* x y ...)
</span>                    <span class="keyword">if</span> op == <span class="string">'*'</span> <span class="keyword">then</span>
                        <span class="keyword">if</span> npe == <span class="number">0</span> <span class="keyword">then</span> <span class="keyword">return</span> <span class="number">0</span>
                        <span class="keyword">elseif</span> npe == <span class="number">1</span> <span class="keyword">then</span> <span class="comment">-- identity
</span>                            <span class="keyword">if</span> #pe == <span class="number">1</span> <span class="keyword">then</span> <span class="keyword">return</span> pe[<span class="number">1</span>] <span class="keyword">else</span> npe = <span class="keyword">nil</span> <span class="keyword">end</span>
                        <span class="keyword">end</span>
                    <span class="keyword">else</span> <span class="comment">-- special cases for +
</span>                        <span class="keyword">if</span> npe == <span class="number">0</span> <span class="keyword">then</span> <span class="comment">-- identity
</span>                            <span class="keyword">if</span> #pe == <span class="number">1</span> <span class="keyword">then</span> <span class="keyword">return</span> pe[<span class="number">1</span>] <span class="keyword">else</span> npe = <span class="keyword">nil</span> <span class="keyword">end</span>
                        <span class="keyword">end</span>
                    <span class="keyword">end</span>
                <span class="keyword">end</span>
                <span class="comment">-- build up the final arguments
</span>                <span class="keyword">local</span> res = {}
                <span class="keyword">if</span> npe <span class="keyword">then</span> <span class="function-name">append</span>(res,npe) <span class="keyword">end</span>
                <span class="keyword">for</span> val,count <span class="keyword">in</span> <span class="global">pairs</span>(<span class="function-name">count_map</span>(pe,equals)) <span class="keyword">do</span>
                    <span class="keyword">if</span> count &gt; <span class="number">1</span> <span class="keyword">then</span>
                        <span class="keyword">if</span> op == <span class="string">'*'</span> <span class="keyword">then</span> val = val ^ count
                        <span class="keyword">else</span> val = val * count
                        <span class="keyword">end</span>
                    <span class="keyword">end</span>
                    <span class="function-name">append</span>(res,val)
                <span class="keyword">end</span>
                <span class="keyword">if</span> #res == <span class="number">1</span> <span class="keyword">then</span> <span class="keyword">return</span> res[<span class="number">1</span>] <span class="keyword">end</span>
                <span class="keyword">return</span> <span class="function-name">PE</span>{op=op,<span class="function-name">unpack</span>(res)}
            <span class="keyword">elseif</span> op == <span class="string">'^'</span> <span class="keyword">then</span>
                <span class="keyword">if</span> args[<span class="number">2</span>] == <span class="number">1</span> <span class="keyword">then</span> <span class="keyword">return</span> args[<span class="number">1</span>] <span class="keyword">end</span> <span class="comment">-- identity
</span>                <span class="keyword">if</span> args[<span class="number">2</span>] == <span class="number">0</span> <span class="keyword">then</span> <span class="keyword">return</span> <span class="number">1</span> <span class="keyword">end</span>
            <span class="keyword">end</span>
            <span class="keyword">return</span> <span class="function-name">PE</span>{op=op,<span class="function-name">unpack</span>(args)}
        <span class="keyword">end</span>
    <span class="keyword">else</span>
        <span class="keyword">return</span> e
    <span class="keyword">end</span>
<span class="keyword">end</span>

<span class="keyword">function</span> <span class="function-name">expand</span> (e)
    <span class="keyword">if</span> <span class="function-name">isPE</span>(e) <span class="keyword">and</span> e.op == <span class="string">'*'</span> <span class="keyword">and</span> <span class="function-name">isPE</span>(e[<span class="number">2</span>]) <span class="keyword">and</span> e[<span class="number">2</span>].op == <span class="string">'+'</span> <span class="keyword">then</span>
        <span class="keyword">local</span> a,b = e[<span class="number">1</span>],e[<span class="number">2</span>]
        <span class="keyword">return</span> <span class="function-name">expand</span>(b[<span class="number">1</span>]*a) + <span class="function-name">expand</span>(b[<span class="number">2</span>]*a)
    <span class="keyword">else</span>
        <span class="keyword">return</span> e
    <span class="keyword">end</span>
<span class="keyword">end</span>

<span class="keyword">function</span> <span class="function-name">isnumber</span> (x)
    <span class="keyword">return</span> <span class="global">type</span>(x) == <span class="string">'number'</span>
<span class="keyword">end</span>

<span class="comment">-- does this PE contain a reference to x?
</span><span class="keyword">function</span> <span class="function-name">references</span> (e,x)
    <span class="keyword">if</span> <span class="function-name">isPE</span>(e) <span class="keyword">then</span>
        <span class="keyword">if</span> e.op == <span class="string">'X'</span> <span class="keyword">then</span> <span class="keyword">return</span> x.repr == e.repr
        <span class="keyword">else</span>
            <span class="keyword">return</span> <span class="function-name">find_if</span>(e,references,x)
        <span class="keyword">end</span>
    <span class="keyword">else</span>
        <span class="keyword">return</span> <span class="keyword">false</span>
    <span class="keyword">end</span>
<span class="keyword">end</span>

<span class="keyword">local</span> <span class="keyword">function</span> <span class="function-name">muli</span> (args)
    <span class="keyword">return</span> <span class="function-name">PE</span>{op=<span class="string">'*'</span>,<span class="function-name">unpack</span>(args)}
<span class="keyword">end</span>

<span class="keyword">local</span> <span class="keyword">function</span> <span class="function-name">addi</span> (args)
    <span class="keyword">return</span> <span class="function-name">PE</span>{op=<span class="string">'+'</span>,<span class="function-name">unpack</span>(args)}
<span class="keyword">end</span>

<span class="keyword">function</span> <span class="function-name">diff</span> (e,x)
    <span class="keyword">if</span> <span class="function-name">isPE</span>(e) <span class="keyword">and</span> <span class="function-name">references</span>(e,x) <span class="keyword">then</span>
        <span class="keyword">local</span> op = e.op
        <span class="keyword">if</span> op == <span class="string">'X'</span> <span class="keyword">then</span>
            <span class="keyword">return</span> <span class="number">1</span>
        <span class="keyword">else</span>
            <span class="keyword">local</span> a,b = e[<span class="number">1</span>],e[<span class="number">2</span>]
            <span class="keyword">if</span> op == <span class="string">'+'</span> <span class="keyword">then</span> <span class="comment">-- differentiation is linear
</span>                <span class="keyword">local</span> args = <span class="function-name">imap</span>(diff,e,x)
                <span class="keyword">return</span> <span class="function-name">balance</span>(<span class="function-name">addi</span>(args))
            <span class="keyword">elseif</span> op == <span class="string">'*'</span> <span class="keyword">then</span> <span class="comment">-- product rule
</span>                <span class="keyword">local</span> res,d,ee = {}
                <span class="keyword">for</span> i = <span class="number">1</span>,#e <span class="keyword">do</span>
                    d = <span class="function-name">fold</span>(<span class="function-name">diff</span>(e[i],x))
                    <span class="keyword">if</span> d ~= <span class="number">0</span> <span class="keyword">then</span>
                        ee = {<span class="function-name">unpack</span>(e)}
                        ee[i] = d
                        <span class="function-name">append</span>(res,<span class="function-name">balance</span>(<span class="function-name">muli</span>(ee)))
                    <span class="keyword">end</span>
                <span class="keyword">end</span>
                <span class="keyword">if</span> #res &gt; <span class="number">1</span> <span class="keyword">then</span> <span class="keyword">return</span> <span class="function-name">addi</span>(res)
                <span class="keyword">else</span> <span class="keyword">return</span> res[<span class="number">1</span>] <span class="keyword">end</span>
            <span class="keyword">elseif</span> op == <span class="string">'^'</span> <span class="keyword">and</span> <span class="function-name">isnumber</span>(b) <span class="keyword">then</span> <span class="comment">-- power rule
</span>                <span class="keyword">return</span> b*x^(b-<span class="number">1</span>)
            <span class="keyword">end</span>
        <span class="keyword">end</span>
    <span class="keyword">else</span>
        <span class="keyword">return</span> <span class="number">0</span>
    <span class="keyword">end</span>
<span class="keyword">end</span></pre>


</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.5.0</a></i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>
